#!/usr/bin/env bash

SERVICE_NAME=$1
SERVICE_OPTIONS="${@:2}"

if [ -z "$SERVICE_NAME" ]; then
    echo "Usage: $0 [SERVICE] [SERVICE_OPTIONS]"
    echo "For example: $0 web --debug --webpack"
    exit -1
fi

COMMAND=""
case "$SERVICE_NAME" in
"web")
    COMMAND="python3 querybook/server/runweb.py"
    ;;
"worker")
    COMMAND="watchmedo auto-restart -d querybook -p '*.py' -R -- celery -A tasks.all_tasks worker --loglevel=DEBUG"
    ;;
"scheduler")
    COMMAND="watchmedo auto-restart -d querybook -p '*.py' -R -- celery -A tasks.all_tasks beat -S scheduler.DatabaseScheduler --loglevel=INFO"
    ;;
"worker_scheduler") # FIXME: this doesn't schedule task as desired
    COMMAND="watchmedo auto-restart -d querybook -p '*.py' -R -- celery -A tasks.all_tasks worker --beat --scheduler scheduler.DatabaseScheduler --loglevel=DEBUG"
    ;;

"prod_web")
    # gunicorn doesn't work with websocket
    # COMMAND="gunicorn -c querybook/server/gunicorn_conf.py app.server:flask_app"
    COMMAND="uwsgi --ini uwsgi.ini"
    ;;
"prod_worker")
    COMMAND="celery -A tasks.all_tasks worker"
    ;;
"prod_scheduler")
    COMMAND="celery -A tasks.all_tasks beat -S scheduler.DatabaseScheduler"
    ;;
"webpack")
    COMMAND="npx webpack"
    ;;
"flower")
    COMMAND="celery -A tasks.all_tasks flower"
    ;;
esac

if [ -z "$COMMAND" ]; then
    echo "Service Unavailable"
    exit -1
fi

COMMAND="${COMMAND} ${SERVICE_OPTIONS}"
echo "Running: $COMMAND"
eval $COMMAND
